package com.hbnu.controller;
import com.hbnu.dao.DoorMapper;
import com.hbnu.pojo.Door;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.InputStream;
import java.util.List;

@Controller
public class TestController {
        // 测试springmvc
        @RequestMapping("/hello")
        public String hello() {
            return "test";
        }
    // 测试mybatis开发环境
    public static void main(String[] args) throws Exception {
        // 1. 读取mybatis-config.xml核心文件
        try (InputStream in = Resources.getResourceAsStream("mybatis/mybatis-config.xml")) {
            // 2. 获取SqlSessionFactory工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            // 3. 使用try-with-resources管理SqlSession对象
            try (SqlSession session = factory.openSession()) {
                // 4. 获取DoorMapper接口的实例
                DoorMapper mapper = session.getMapper(DoorMapper.class);
                // 5. 调用findAll方法查询所有门店信息
                List<Door> list = mapper.findAll();
                // 6. 遍历所有门店信息
                for (Door door : list) {
                    System.out.println(door);
                }
            }
        }
    }
    /**
     * 自动装配：由spring自动为属性赋值(对象)
     */
    @Autowired
    private DoorMapper mapper;
    @RequestMapping("/testssm")
    public String testSSM() {
        // 1.调用findAll方法查询所有门店信息
        List<Door> list = mapper.findAll();
        // 2.遍历所有门店信息
        for (Door door : list) {
            System.out.println(door);
        }
        return "test";
    }
}
